package com.dk.client;

import com.alibaba.fastjson2.JSONObject;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    private static Socket socket;
    static boolean socket_status=false;
    public static void main(String[] args) {
        while (!socket_status){
            connect();
             try {
                Thread.sleep(3000);
            }catch (Exception e){
                e.printStackTrace();
            }
        }

    }
    private static void connect() {
        try{
            socket=new Socket("127.0.0.1", 9001);
            socket_status=true;
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
            ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
            new Thread(new Client_listen(socket, objectInputStream)).start();
            new Thread(new Client_send(socket, objectOutputStream)).start();
            new Thread(new Client_heart(socket, objectOutputStream)).start();  // 添加这一行

        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
    public static void reconnect(){
        while (true) {
            System.out.println("正在尝试重新连接");
            connect();
            try {
                Thread.sleep(3000);
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }

}

class Client_listen implements Runnable{
    private Socket socket;
    private ObjectInputStream objectInputStream;
    Client_listen(Socket socket, ObjectInputStream objectInputStream){
        this.socket=socket;
        this.objectInputStream=objectInputStream;
    }
    @Override
    public void run() {
        while(true){
            try{
                System.out.println(objectInputStream.readObject());
            }
            catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
class Client_send implements Runnable{
    private Socket socket;
    private ObjectOutputStream objectOutputStream;
    Client_send(Socket socket, ObjectOutputStream objectOutputStream){
        this.socket=socket;
        this.objectOutputStream=objectOutputStream;
    }
    @Override
    public void run() {
        try{
            Scanner scanner = new Scanner(System.in);
            while (true){
                System.out.println("客户端，请输入：");
                String string = scanner.nextLine();

                JSONObject jsonObject = new JSONObject();
                jsonObject.put("type", "msg");
                jsonObject.put("content", string);
                objectOutputStream.writeObject(jsonObject);
                objectOutputStream.flush();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

class Client_heart implements Runnable{
    private Socket socket;
    private ObjectOutputStream objectOutputStream;
    Client_heart(Socket socket, ObjectOutputStream objectOutputStream){
        this.socket=socket;
        this.objectOutputStream=objectOutputStream;
    }
    @Override
    public void run() {
        try{
            while (true){
                Thread.sleep(3000);
                System.out.println("心跳检测");
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("type", "heart");
                jsonObject.put("content", "心跳...");
                objectOutputStream.writeObject(jsonObject);
                objectOutputStream.flush();
             }
        }catch (Exception e){
            e.printStackTrace();
            try{
                socket.close();
                Client.socket_status=false;
                Client.reconnect();
            }
            catch (Exception ee){
                ee.printStackTrace();
            }
    }
}}